function [ErrTL,ErrLogit,diffvarLL1, diffvarU, covar]=combinederr(lambda,x,y,ind,distr)

n=length(y);

% Logit Level 1

l=lambda;
predLogit=zeros(n,3);
for g=1:n
    sumpay=sum(x{g},2);
    predLogit(g,:)=exp(l*sumpay);
    predLogit(g,:)=predLogit(g,:)./sum(predLogit(g,:));
end
errLogit=zeros(n,1);
for i=1:n
    z=predLogit(i,str2double(y(i)));
    if z==0
        errLogit(i)=-log(0.001);
    else
        errLogit(i)=-log(z);
    end
end
ErrLogit=mean(errLogit);


% Table Lookup
predTL=zeros(n,3);
for i=1:n
    predTL(i,:)=distr(ind(i),:);
end

errTL=zeros(n,1);
for i=1:n
    z=predTL(i,str2double(y(i)));
    if z==0
        errTL(i)=-log(0.001);
    else
        errTL(i)=-log(z);
    end
end
ErrTL=mean(errTL);


% Sample Variance for Difference in Test Errors

diffvarLL1 = var(errTL - errLogit);
diffvarU = var( -log(1/3) - errTL);
covar = cov((errTL - errLogit), (errTL + log(1/3)));
covar=covar(1,2);

